package com.kjj.org.web.controller;

import com.kjj.basic.util.AjaxResult;
import com.kjj.basic.util.PageList;
import com.kjj.org.domain.Department;
import com.kjj.org.query.DepartmentQuery;
import com.kjj.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门API",description = "包含部门crud")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    //put add update
    @PutMapping
    @ApiOperation(value = "部门的添加或修改",notes = "有id就是修改，没有id就是添加")
    public AjaxResult addOrUpdate(@ApiParam(value = "部门对象",readOnly = true) @RequestBody Department department){
        try {
            if (department.getId()==null)
                departmentService.add(department);
            else
                departmentService.update(department);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("更新或添加失败" + e.getMessage());
        }
    }
    //delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门的删除",notes = "传入部门id进行删除")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }
    @PatchMapping("/batch")
    @ApiOperation(value = "部门的批量删除",notes = "传入选项id进行删除")
    public AjaxResult deleteBatch(@RequestBody List<Long> ids) {    //请求体的返回值需要需要加上@RequestBody
        try {
            ids.forEach(System.out::println);
            departmentService.delBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }
    }

    //get 一个
    @GetMapping("/{id}")
    @ApiOperation(value = "部门的获取",notes = "根据所传入的id获取对应的部门信息")
    public AjaxResult getById(@PathVariable("id") Long id){
        try {
            Department department = departmentService.getById(id);
            return AjaxResult.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败" + e.getMessage());
        }
    }
    //get 所有
    @GetMapping
    @ApiOperation(value = "获取所有部门",notes = "返回所有部门信息")
    public AjaxResult getAll(){
        try {
            List<Department> department = departmentService.getAll();
            return AjaxResult.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败" + e.getMessage());
        }
    }
    //分页 query requestbody   POST
    @PostMapping
    @ApiOperation(value = "部门的分页查询",notes = "传入分页当前页和页面大小参数")
    public AjaxResult queryPage(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> list = departmentService.queryPage(query);
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页失败" + e.getMessage());
        }
    }
    //获取部门树，只有两级
    @GetMapping("/tree")
    public AjaxResult loadTree(){

        try {
            List<Department> list = departmentService.getTree();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取部门树失败！"+e.getMessage());
        }
    }
}
